<template>
  <div>
    <!-- 按关键词查询 -->
    <el-card shadow="hover" style="margin-bottom: 20px">
      <div slot="header" class="clearfix">
        <span>按物资关键词查询位置</span>
      </div>
      <el-form :inline="true" :model="keywordSearchForm" @submit.native.prevent="handleKeywordSearch">
        <el-form-item label="物资关键词">
          <el-input v-model="keywordSearchForm.keyword" placeholder="例如：锤子"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleKeywordSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 按类型+类目查询 -->
    <el-card shadow="hover" style="margin-bottom: 20px">
      <div slot="header" class="clearfix">
        <span>按类型+类目ID查询位置</span>
      </div>
      <el-form :inline="true" :model="typeSearchForm">
        <el-form-item label="类型ID">
          <el-select v-model="typeSearchForm.targetTypeId" placeholder="请选择类型">
            <el-option label="设备" :value="1"></el-option>
            <el-option label="工具" :value="2"></el-option>
            <el-option label="物料" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类目ID">
          <el-input type="number" v-model.number="typeSearchForm.targetCategoryId" placeholder="请输入类目ID"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleTypeSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 查询结果 -->
    <el-card shadow="hover">
      <div slot="header" class="clearfix">
        <span>查询结果</span>
      </div>
      <el-table :data="searchResults" style="width: 100%" v-loading="loading">
        <el-table-column prop="materialName" label="物资名称"></el-table-column>
        <el-table-column prop="materialType" label="物资类型"></el-table-column>
        <el-table-column prop="shelfId" label="货架ID"></el-table-column>
        <el-table-column prop="shelfLayerId" label="货架层数ID"></el-table-column>
        <el-table-column prop="shelfCellId" label="货架格子ID"></el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import materialApi from '@/api/material';

export default {
  name: 'MaterialLocation',
  data() {
    return {
      loading: false,
      keywordSearchForm: {
        keyword: ''
      },
      typeSearchForm: {
        targetTypeId: null,
        targetCategoryId: null
      },
      searchResults: []
    };
  },
  methods: {
    handleApiResponse(res) {
      if (res && (res.code === 200 || res.success === true)) {
        const results = res.data?.locationList || [];
        this.searchResults = this.normalizeResults(results);
        if (this.searchResults.length === 0) {
          this.$message.info('未查询到相关物资位置信息');
        } else {
          this.$message.success(res.message || `查询成功，共${this.searchResults.length}条记录`);
        }
      } else {
        this.$message.error(res.message || '查询失败');
        this.searchResults = [];
      }
    },
    // 统一处理返回结果的字段，使其能被表格正确展示
    normalizeResults(results) {
        if (!Array.isArray(results)) return [];
        return results.map(item => ({
            materialName: item.targetCategoryName || 'N/A',
            materialType: item.targetTypeName || 'N/A',
            shelfId: item.shelfId || 'N/A',
            shelfLayerId: item.shelfLayerId || 'N/A',
            shelfCellId: item.shelfCellId || 'N/A',
        }));
    },
    // 按关键词搜索
    handleKeywordSearch() {
      if (!this.keywordSearchForm.keyword) {
        this.$message.warning('请输入物资关键词');
        return;
      }
      this.loading = true;
      materialApi.searchByKeyword(this.keywordSearchForm)
        .then(res => {
          this.handleApiResponse(res.data);
        })
        .catch(err => {
          this.$message.error('请求失败: ' + err.message);
          this.searchResults = [];
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 按类型+类目搜索
    handleTypeSearch() {
      if (!this.typeSearchForm.targetTypeId || !this.typeSearchForm.targetCategoryId) {
        this.$message.warning('请选择类型并输入类目ID');
        return;
      }
      this.loading = true;
      materialApi.searchByTypeAndCategory(this.typeSearchForm)
        .then(res => {
          this.handleApiResponse(res.data);
        })
        .catch(err => {
          this.$message.error('请求失败: ' + err.message);
          this.searchResults = [];
        })
        .finally(() => {
          this.loading = false;
        });
    }
  }
};
</script>

<style scoped>
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
</style>